
#include "main_inc.h"

#ifndef DBFILE_H
#define DBFILE_H

#define SYS_LINES   80
#define SYS_BAT_COUNT    64
#define USERNAME_LEN     12

u32 dataCRC(void *data, u32 len);
extern string g_ArchPath;

#pragma pack ( push, 1)

struct dbf_header
{
	i16 checkbits;                  // must be 0x4D7A
	time_t arch_creat_time;         // time & date when file was created
	time_t arch_creat_time_ext;     // same as above, except external time
	u32 first_bat_addres;           //
	u32 arch_blocks;                // blocks count
	u32 arch_bats;
	u32 flags;
	u32 crc_checksum;               // crc of the header
	u32 end_addr;

	u32 crc();
};

struct dbf_block
{
	dbf_block()
	{
		bl_time = 0;
		bl_type = 0;
		bl_time_ext  = 0;
		bl_size = 0;
		bl_addr = 0;
		bl_crc = 0;
		search_for_next = 0;
		empty = 1;
	}
	u32 nmbr;
	u8 bl_type;
	time_t bl_time;
	time_t bl_time_ext;
	u32 search_for_next : 1;        /// is this time duplicated ///($$$!!!$$$)
	u32 empty : 1;
	u32 bl_addr;
	u32 bl_size;
	u32 bl_crc;                     /// block crc
	u32 data_crc;                        /// data crc

	u32 crc();
};

struct dbf_bat                      // block allocation table
{
	i32 bat_nmbr;
	i32 bat_addr;                   // addr of this bat
	i32 used_blocks;
	i32 indx_prev;                  // index of previous BAT in file (-1 if don't exist)
	i32 indx_next;                  // index of next BAT in file (-1 if don't exist)
	dbf_block block[SYS_BAT_COUNT];
	u32 bat_crc;
	u32 crc();

};

struct dbf_measuments
{
	time_t arch_time;
	time_t arch_ext_time;
	double values[SYS_LINES];
	u32 flags[SYS_LINES];
};

struct dbf_logon
{
	time_t arch_time;
	time_t arch_ext_time;
	char username[USERNAME_LEN];
};

struct dbf_binary_readings
{
	time_t arch_time;
	time_t arch_ext_time;
	i8      bin_input[6];           // state have max 8 states. 3bits * 16 inputs give 48bits - 6 * i8
	i8      bin_output[3];          // max 8 states * 8 outputs
	i8 aku_volt;
	i8 flags;

};

struct dbf_config
{
	time_t arch_time;
	time_t arch_ext_time;
	i16 line_num;
	char sensor_type[16];
	char sensor_med[16];
	char desc_1[256];
	char desc_2[256];
	double al1_val;
	double al2_val;
	u8 status;
	u32 matrix_index;
	u32 flags;
	char username[USERNAME_LEN];
	u32 hash;
};

struct dbf_event
{
	time_t arch_time;
	time_t arch_ext_time;
	u32 event_code;
	u32 event_flag;
};

struct dbf_comment
{
	time_t comm_time_start;
	time_t comm_time_start_ex;
	time_t comm_time_end;
	char username[USERNAME_LEN];
	char comment1[256];
	char comment2[256];
};

#define SRCH_MATCH      0x0001
#define SRCH_FROMTO     0x0002
#define SRCH_TYPEMATCH  0x0004
#define SRCH_EXTTIME    0x0008

struct dbf_search_result
{
    void *ident;
    u32 result_number;
    u32 bat_number;
    u32 bat_addr;
    u32 block_count;              // block number in bat
    u32 block_addr;
    u32 block_size;
    u8 block_type;
    time_t sysT;
    time_t extT;
};

#pragma pack (pop)

#endif

////////////////////






